package com.tenglong.teacher.ui.publish.text;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;

import com.cuotibao.zhuoxiao.basics.utils.DensityUtil;
import cn.hzw.doodle.DoodlePen;
import cn.hzw.doodle.DoodleRotatableItemBase;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleColor;


/**
 * Created by admin on 2019/6/20.
 */

public class DoodleStoreText extends DoodleRotatableItemBase {
    private StaticLayout myStaticLayout;
    private Rect mRect = new Rect();
    private String mText;
    private TextPaint myTextPaint;
    private float mX;
    private float mY;
    public DoodleStoreText(IDoodle doodle, String text, float size, IDoodleColor color, float x, float y) {
        super(doodle, -doodle.getDoodleRotation(), x, y);
        mX = x;
        mY = y;
        setPen(DoodlePen.TEXT);
        mText = text;
        setSize(size);
        setColor(color);

    }

    public String getText() {
        return mText;
    }

    public void setText(String text) {
        mText = text;
        resetBounds(mRect);
        setPivotX(getLocation().x + mRect.width() / 2);
        setPivotY(getLocation().y + mRect.height() / 2);
        resetBoundsScaled(getBounds());

        refresh();
    }

    @Override
    public void resetBounds(Rect rect) {
        String text = getText();
        if (TextUtils.isEmpty(text)) {
            return;
        }
        myTextPaint = new TextPaint();
        myTextPaint.setAntiAlias(true);
        myTextPaint.setTextSize(getSize());
        myTextPaint.setColor(Color.RED);
        int width = DensityUtil.dp2px(196);
        if(text.length()<14){
            width=DensityUtil.dp2px(14*text.length())+20;
        }
        myStaticLayout = new StaticLayout(text, myTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        float height = myStaticLayout.getHeight();
        rect.left = (int) (mX-width/2);
        rect.top = (int) (mY-height/2);
        rect.bottom = (int) (mY+height/2);
        rect.right = (int) (mX+width/2);
        rect.offsetTo(0,0);
    }

    @Override
    public void doDraw(Canvas canvas) {
        getColor().config(this, myTextPaint);
        myTextPaint.setTextSize(DensityUtil.dp2px(14));
        myTextPaint.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.translate(0, 0);
//        canvas.translate(getBounds().centerX()-getBounds().width()/2, getBounds().centerY()-getBounds().height()/2);
        myStaticLayout.draw(canvas);
        canvas.restore();
    }

}



